package com.example.myapplicationtest;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Appmain extends AppCompatActivity implements View.OnClickListener {
String path3="https://www.wanandroid.com/user/logout/json";
Intent intent;
AlertDialog dialog;
Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appmain);
        dialog=new AlertDialog.Builder(this).create();
        intent = new Intent(this,MainActivity.class);
       button=(Button)findViewById(R.id.button);
       button.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {

if(R.id.button==v.getId()) {
    new Thread() {
        private HttpURLConnection coon2;

        @Override
        public void run() {

            try {
                URL url = new URL(path3);
                coon2= (HttpURLConnection) url.openConnection();
                coon2.setRequestMethod("GET");
                coon2.setReadTimeout(5000);
                coon2.setConnectTimeout(5000);
                    InputStream is = coon2.getInputStream();
                    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = is.read(buffer)) != -1) {
                        outStream.write(buffer, 0, len);
                    }
                    is.close();
                    byte[] data1 = outStream.toByteArray();
                    ;
                    String html = new String(data1, "UTF-8");
                   Gson gson=new Gson();
                quitBean quitBean = gson.fromJson(html, quitBean.class);

                if ( quitBean.getErrorCode()==0) {
                        Message msg = new Message();
                        msg.what = 1;
                        msg.obj = quitBean.getErrorCode();
                        handler.sendMessage(msg);
                    } else {
                        Message msg1 = new Message();
                        msg1.what = 0;
                        msg1.obj =quitBean.getErrorMsg();
                        handler.sendMessage(msg1);

                    }


            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }.start();
}

        }


    private Handler handler=new Handler(){
        public void handleMessage(android.os.Message msg){
            if(msg.what==1){
                int  a =(Integer) msg.obj;
                startActivity(intent);

            }else{
                String ss=(String)msg.obj;
                dialog.setTitle("消息提示框");
                dialog.setMessage(ss);
                dialog.show();

            }
        }


    };



}